package packets;

import java.awt.List;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;

import org.jnetpcap.nio.JBuffer;
import org.jnetpcap.packet.JHeader;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.JRegistry;
import org.jnetpcap.packet.RegistryHeaderErrors;
import org.jnetpcap.packet.annotate.Bind;
import org.jnetpcap.packet.annotate.Field;
import org.jnetpcap.packet.annotate.Header;
import org.jnetpcap.packet.annotate.HeaderLength;

/**
 * Diese Klasse repräsentiert das OSPF Hello Paket und definiert Methoden, die Felder des Pakets zurück geben.
 * @author pascal
 *
 */
@Header(name="OSPFHello", description="OSPFHeader")
public class OSPFHello extends JHeader {

private ArrayList<Neighbor> neighbors = new ArrayList<Neighbor>();	

public ArrayList<Neighbor> getNeighbors(){
	return this.neighbors;
}

@Field(offset=0, length = 4)
public long networkMask(){
	return getUInt(0);
}
	
@Field(offset=4*8, length = 2*8)
public byte[] helloIntervall(){
	return getByteArray(0, 2);
}

@Field(offset= 6*8, length = 1*8)
public String options(){
	return getUTF8String(2, 1);  
}

@Field(offset = 7*8, length=1*8)
public int routerPriority(){
	return getUByte(7);
}

@Field(offset = 8*8, length=4*8)
public int deadInterval(){
	return getUByte(8);
}

@Field(offset = 12*8, length=4*8)
public byte[] designatedRouter(){
	return getByteArray(12, 4);
}

@Field(offset = 16*8, length = 4*8)
public byte[] backupDesignatedRouter(){
	return getByteArray(16, 4);
}



	@Bind(to=OSPF.class)
	public static boolean bindToOSPF(JPacket packet, OSPF ospf){
		return ospf.type() == 0x01;
	}
	
	@HeaderLength
	public static int headerLength(JBuffer buffer, int offset){
		return buffer.size() - offset;
	}

	@Override
	protected void decodeHeader() {
				
			int offsetIntoHeader = 20;
			int bytesRemaining = this.size() - offsetIntoHeader;
			
			while(bytesRemaining > 0){
				Neighbor neighbor = new Neighbor();
				neighbors.add(neighbor);
				
				neighbor.peer(this, offsetIntoHeader, 4);
				
				offsetIntoHeader = offsetIntoHeader+4;
				
			}
				
				
		}
	

	static {
		try {
			JRegistry.register(OSPFHello.class);
		}catch(RegistryHeaderErrors e){
			e.printStackTrace();
		}
	}

}
